﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Media;

namespace iNet.Media.Imaging
{
	/// <summary>
	/// 實作 IMultiImage 介面的類別。
	/// </summary>
	public class MultiImage : MediaBase, IMultiImage
	{
		#region 欄位
		IList<IImage> _Images;
		IMediaFormat _RawFormat;
		#endregion
		#region 建構子
		/// <summary>
		/// 初始化新的 MultiImage 執行個體。
		/// </summary>
		/// <param name="images">子影像集合。</param>
		/// <param name="metadata">中繼資料。</param>
		/// <param name="source">影像的儲存來源。</param>
		/// <param name="rawFormat">原始影像格式。</param>
		/// <param name="isReadOnly">是否建立為唯讀影像。</param>
		public MultiImage(IList<IImage> images, IMetadata metadata, MediaStorage source, IMediaFormat rawFormat, bool isReadOnly)
			: base(source, isReadOnly, metadata)
		{
			//初始化
			_RawFormat = rawFormat;

			//建立集合
			if (images != null)
			{
				if (isReadOnly && !images.IsReadOnly)
					_Images = new ReadOnlyCollection<IImage>(images);
				else
					_Images = images;
			}
			else
			{
				if (isReadOnly)
					_Images = new IImage[0];
				else
					_Images = new List<IImage>();
			}
		}
		#endregion
		#region Clone
		/// <summary>
		/// 建立可修改的影像副本。
		/// </summary>
		/// <returns>影像副本。</returns>
		/// <exception cref="ObjectDisposedException">影像已經處置。</exception>
		public override MediaBase Clone()
		{
			//確認狀態
			if (this.IsDisposed)
				throw new ObjectDisposedException("MultiImage");

			//建立唯讀副本
			return new MultiImage((_Images is ICloneable ? (_Images as ICloneable).Clone() as IList<IImage> : _Images), this.Metadata, this.MediaStorage, _RawFormat, false);
		}
		#endregion
		#region GetAsReadOnly
		/// <summary>
		/// 取得唯讀版本的影像。
		/// </summary>
		/// <returns>唯讀版本的影像。</returns>
		/// <exception cref="ObjectDisposedException">影像已經處置。</exception>
		public override IMedia GetAsReadOnly()
		{
			//確認狀態
			if (this.IsDisposed)
				throw new ObjectDisposedException("MultiImage");

			//建立唯讀副本
			return new MultiImage((_Images is ICloneable ? (_Images as ICloneable).Clone() as IList<IImage> : _Images), this.Metadata, this.MediaStorage, _RawFormat, true);
		}
		#endregion
		#region Images
		/// <summary>
		/// 取得子影像集合。
		/// </summary>
		public virtual IList<IImage> Images
		{
			get
			{
				return _Images;
			}
		}
		#endregion
		#region RawFormat
		/// <summary>
		/// 取得原始影像格式。
		/// </summary>
		public IMediaFormat RawFormat
		{
			get
			{
				return _RawFormat;
			}
		}
		#endregion
		#region ToImageSource
		/// <summary>
		/// 取得表示此影像的 System.Windows.Media.ImageSource 物件。
		/// </summary>
		/// <returns>System.Windows.Media.ImageSource 物件。</returns>
		/// <exception cref="ObjectDisposedException">影像已經處置。</exception>
		/// <exception cref="InvalidOperationException">沒有可用的子影像。</exception>
		public virtual ImageSource ToImageSource()
		{
			//確認狀態
			if (this.IsDisposed)
				throw new ObjectDisposedException("MultiImage");

			//轉成 ImageSource
			for (int i = _Images.Count - 1; i >= 0; --i)
			{
				IImage image = _Images[i];
				if (image != null)
					return image.ToImageSource();
			}

			//擲出例外
			throw new InvalidOperationException();
		}
		#endregion
	}
}
